package com.example.petplatform.service;

import com.example.petplatform.dao.UsersDao;
import com.example.petplatform.entity.Param.Params;
import com.example.petplatform.entity.Users;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UsersService {
    @Resource
    private UsersDao usersDao;

    public List<Users> getAdmin() {
        return usersDao.getAdmin();
    }

    public PageInfo<Users> getSearch(Params params) {
        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        List<Users> list = usersDao.getSearch(params);
        return PageInfo.of(list);
    }

    public List<Users> addAdmin(Users user) {
        // Check for duplicate username
        Users existingUser = usersDao.getUserByUsername(user.getUsername());
        if (existingUser != null) {
            throw new RuntimeException("用户名已存在，请重新输入！");
        }
        usersDao.insertSelective(user);
        return usersDao.getAdmin();
    }

    public List<Users> updateAdmin(Users user) {
        usersDao.updateByPrimaryKeySelective(user);
        return usersDao.getAdmin();
    }

    public List<Users> deleteAdmin(Integer id) {
        usersDao.deleteByPrimaryKey(id);
        return usersDao.getAdmin();
    }
}
